// pages/user/addrole.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info:{},
    data:{},
    memuIds:[],
    authIds:[]
  },
 
  chooseRoleList:function(e){
    console.log(e.currentTarget.dataset);
    var data = e.currentTarget.dataset;
    //console.log(data);
    var id = data.id;
    var arr = this.data.authIds;
  
    var index = arr.indexOf(id);
    var list = this.data.data.roleList;
    if (index>-1){
      arr.splice(index, 1);
      list[data.idx]['scat'][data.sidx]['checked']=0;
    }else{
      arr.push(id);
      list[data.idx]['scat'][data.sidx]['checked'] = 1;
    }
    this.data.authIds = arr;
    this.data.data.roleList = list;
    this.setData({ data:this.data.data});
    //console.log(arr); 
  },

  chooseMenuList: function (e) {
    //console.log(e.currentTarget.dataset);
    var data = e.currentTarget.dataset;
    //console.log(data);
    var id = data.id;
    var arr = this.data.memuIds;

    var index = arr.indexOf(id);
    var list = this.data.data.menuList;
    if (index > -1) {
      arr.splice(index, 1);
      list[data.idx]['scat'][data.sidx]['checked'] = 0;
    } else {
      arr.push(id);
      list[data.idx]['scat'][data.sidx]['checked'] = 1;
    }
    this.data.memuIds = arr;
    this.data.data.menuList = list;
    this.setData({ data: this.data.data });
    //console.log(arr);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_info = wx.getStorageSync('user_info');
    this.setData({ user_info: user_info, id: options.id });
    this.data.id = 17;
    var that = this;
    app.api("api/role/index",{
      id:this.data.id
    },function(res){
      if(res.code!=200){
         app.alert(res.msg);
      }
      that.setData({ data: res.data, authIds: res.data.authIds, menuIds: res.data.menuIds});
      // console.log(res);
    })
  },
  btn_cancel:function(){
    wx.navigateBack();
  },

  post:function(e){
    
    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var _this = this;
    app.api("api/role/save", {
      id: this.data.id,
      memuIds: this.data.memuIds,
      authIds: this.data.authIds,
      method: 'POST',
    }, function (res) {
      _this.setData({ 'loading': 0 });
      _this.setData({ 'disabled': 0 });
      var title = "";
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateBack()
      } else {
        app.alert(res.msg);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})